import os, sys
from mako.template import Template
from optparse import OptionParser
def main(opts):
    makotext = '''<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<zebra>
    <pid_file file_path = \"${opts["conf"]}/etc/maple.pid\" />
    <data_dir dir_path = \"${opts["conf"]}/data\" />
    <zone_db_file filename = \"${opts["conf"]}/etc/zone.db\" />
    <options>
        <network>
            <tcp_server work_thread = \"${opts['tcpthr']}\">
                <services>
                % for svr in opts['svr'].split(","):
                     <service ip = \"${svr.split(':')[0]}\" port = \"${svr.split(":")[1]}\" />  
                %endfor
                </services>    
            </tcp_server>
            <udp_server work_thread = \"${opts['udpthr']}\">
                <services>
                % for svr in opts['svr'].split(","):
                     <service ip = \"${svr.split(':')[0]}\" port = \"${svr.split(":")[1]}\" />  
                %endfor
                </services>    
            </udp_server>
        </network>
                <keys>
                   <key name = "key1" algorithm = "HMAC-MD5" secret = "JybkdnSoWYpoHLeF423QPQ==" /> 
                </keys>
        <zone_options filename = \"${opts['conf']}/etc/zones.xml\" />
    </options>
    % if opts['log'] == None:
    <logs use = "no" path = \"${opts['conf']}/log\">
    % else:
    <logs use = "yes" path = \"${opts['conf']}/log\">
    % endif
        <module name="queries" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="db"  use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="error" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="general" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="xfrin" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="xfrout" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="config" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="notify" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="update" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
        <module name="dplus" use = "yes" level = "6" file_size = "10" file_num = "10" realtime = "yes" />
    </logs>
</zebra>
'''
    try:
        os.mkdir(opts['conf'] + '/data')
        os.mkdir(opts['conf'] + '/etc')
        os.mkdir(opts['conf'] + '/log')
    except:
        pass

    if 'udpthr' not in opts:
        opts['udpthr'] = 1
    if 'tcpthr' not in opts:
        opts['tcpthr'] = 1
    if 'log' not in opts:
        opts['log'] = None
    tpl = Template(makotext)
    txt = tpl.render(opts = opts)
    with open(opts['conf'] + '/etc/maple.xml', 'w') as fp:
        fp.write(txt)

def getopt():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-c", "--conf", dest="conf",
        help="specify output maple.xml path")
    parser.add_option("-t", "--tcp_threads", dest="tcpthr", default=1, type=int,
        help="specify the number of threads work for TCP")
    parser.add_option("-u", "--udp_threads", dest="udpthr", default=1, type=int,
        help="specify the number of threads work for UDP")
    parser.add_option("-s", "--ip_port_str", dest="svr", default = "127.0.0.1:53, 0.0.0.0:53",
        help="specify the listen-on address/port")
    parser.add_option("-c", "--log", dest="log", default = None,
        help="specify whether start log or not")

    (options, args) = parser.parse_args()
    
    if(options.conf == None or options.svr == None ):
        parser.print_help()
        sys.exit(1)

    return options

if __name__ == "__main__":
    opts = getopt()
    main(vars(opts))

    zoneopts={}
    zoneopts['path'] = opts.conf + '/data'
    import gen_zone_file
    gen_zone_file.main(zoneopts)
    
